{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import yaml\n",
    "import json\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = '../DM_model/smarthome_AC_story.yml'\n",
    "with open(path, 'r', encoding='utf-8') as f:\n",
    "    dataset = yaml.load(f.read(),Loader=yaml.Loader)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 输入\n",
    "    - Previous system action\n",
    "    - System Action\n",
    "    - Slots\n",
    "    - User intent entities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'intent': 'Control-AC_Open', 'entities': [{'address': '主卧'}, {'device': '空调'}, {'operation': '打开'}]}, {'action': 'action_open_ac'}]\n",
      "1\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'device': '空调'}, {'operation': '打开'}]}, {'action': 'action_open_ac'}, {'intent': 'inform_ac', 'entities': [{'address': '客厅'}, {'device': '空调'}]}, {'action': 'action_open_ac'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'device': '空调'}, {'operation': '打开'}]}, {'action': 'action_open_ac'}, {'intent': 'inform_room', 'entities': [{'address': '客厅'}]}, {'action': 'action_open_ac'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'device': '空调'}, {'operation': '打开'}]}, {'action': 'action_open_ac'}, {'intent': 'inform_range', 'entities': [{'range': '所有'}]}, {'action': 'action_open_ac'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'range': '所有'}, {'device': '空调'}, {'operation': '打开'}]}, {'action': 'action_open_ac'}]\n",
      "1\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'operation': '打开'}, {'device': '空调'}]}, {'action': 'action_open_ac'}, {'intent': 'inform_ac', 'entities': [{'device': '空调'}, {'address': '客厅'}]}, {'action': 'action_open_ac'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'operation': '打开'}, {'device': '空调'}]}, {'action': 'action_open_ac'}, {'intent': 'inform_room', 'entities': [{'address': '客厅'}]}, {'action': 'action_open_ac'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Close', 'entities': [{'address': '客厅'}, {'device': '空调'}, {'operation': '关闭'}]}, {'action': 'action_close_ac'}]\n",
      "1\n",
      "/n\n",
      "[{'intent': 'Control-AC_Close', 'entities': [{'device': '空调'}, {'operation': '关闭'}]}, {'action': 'action_close_ac'}, {'intent': 'inform_ac', 'entities': [{'address': '客厅'}, {'device': '空调'}]}, {'action': 'action_close_ac'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Close', 'entities': [{'range': '所有'}, {'device': '空调'}, {'operation': '关闭'}]}, {'action': 'action_close_ac'}]\n",
      "1\n",
      "/n\n",
      "[{'intent': 'Control-AC_Close', 'entities': [{'operation': '关闭'}, {'device': '空调'}]}, {'action': 'action_close_ac'}, {'intent': 'inform_ac', 'entities': [{'device': '空调'}, {'address': '书房'}]}, {'action': 'action_close_ac'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Close', 'entities': [{'operation': '关闭'}, {'device': '空调'}]}, {'action': 'action_close_ac'}, {'intent': 'inform_room', 'entities': [{'address': '主卧'}]}, {'action': 'action_close_ac'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Close', 'entities': [{'operation': '关闭'}, {'device': '空调'}]}, {'action': 'action_close_ac'}, {'intent': 'inform_range', 'entities': [{'range': '所有'}]}, {'action': 'action_close_ac'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Mode', 'entities': [{'address': '客厅'}, {'device': '空调'}, {'mode': '制热模式'}]}, {'action': 'acmode_form'}]\n",
      "1\n",
      "/n\n",
      "[{'intent': 'Control-AC_Mode', 'entities': [{'mode': '节能模式'}]}, {'action': 'acmode_form'}, {'intent': 'inform_ac', 'entities': [{'address': '客厅'}, {'device': '空调'}]}, {'action': 'acmode_form'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Mode', 'entities': [{'device': '空调'}, {'mode': '节能模式'}]}, {'action': 'acmode_form'}, {'active_loop': 'acmode_form'}, {'intent': 'inform_ac', 'entities': [{'address': '主卧'}, {'device': '空调'}]}, {'action': 'acmode_form'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'operation': '打开'}, {'device': '空调'}, {'address': '书房'}]}, {'action': 'action_open_ac'}, {'intent': 'Control-AC_Mode', 'entities': [{'mode': '节能模式'}]}, {'action': 'acmode_form'}, {'intent': 'Control-AC_Mode', 'entities': [{'mode': '左右扫风'}]}, {'action': 'acmode_form'}]\n",
      "3\n",
      "/n\n",
      "[{'intent': 'Control-AC_Timing', 'entities': [{'time': '一小时'}, {'operation': '关闭'}, {'device': '空调'}, {'address': '主卧'}]}, {'action': 'actiming_form'}]\n",
      "1\n",
      "/n\n",
      "[{'intent': 'Control-AC_Timing', 'entities': [{'time': '一小时'}, {'operation': '关闭'}, {'device': '空调'}]}, {'action': 'actiming_form'}, {'intent': 'inform_ac', 'entities': [{'device': '空调'}, {'address': '主卧'}]}, {'action': 'actiming_form'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Timing', 'entities': [{'time': '一小时'}, {'operation': '关闭'}, {'device': '空调'}]}, {'action': 'actiming_form'}, {'intent': 'inform_room', 'entities': [{'address': '主卧'}]}, {'action': 'actiming_form'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Timing'}, {'action': 'actiming_form'}, {'intent': 'Control-AC_Timing', 'entities': [{'time': '一小时'}, {'operation': '关闭'}, {'device': '空调'}]}, {'action': 'actiming_form'}, {'intent': 'inform_ac', 'entities': [{'device': '空调'}, {'address': '主卧'}]}, {'action': 'actiming_form'}]\n",
      "3\n",
      "/n\n",
      "[{'intent': 'Control-AC_Timing', 'entities': [{'time': '五小时'}, {'operation': '关闭'}, {'home_device': '空调'}]}, {'action': 'actiming_form'}, {'intent': 'inform_room', 'entities': [{'address': '主卧'}]}, {'action': 'actiming_form'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'operation': '打开'}, {'device': '空调'}]}, {'action': 'action_open_ac'}, {'intent': 'inform_room', 'entities': [{'address': '主卧'}]}, {'action': 'action_open_ac'}, {'intent': 'Control-AC_Timing', 'entities': [{'time': '一小时'}, {'operation': '关闭'}]}, {'action': 'actiming_form'}]\n",
      "3\n",
      "/n\n",
      "[{'intent': 'Control-AC_Wind', 'entities': [{'operation': '增大'}, {'target': '风速'}, {'device': '空调'}, {'address': '客厅'}]}, {'action': 'action_controlDeviceWind'}]\n",
      "1\n",
      "/n\n",
      "[{'intent': 'Control-AC_Wind', 'entities': [{'sensorvalue': '最大'}, {'target': '风速'}, {'device': '客厅空调'}, {'address': '客厅'}]}, {'action': 'action_controlDeviceWind'}]\n",
      "1\n",
      "/n\n",
      "[{'intent': 'Control-AC_Wind', 'entities': [{'operation': '调高'}, {'target': '风速'}, {'device': '空调'}]}, {'action': 'action_controlDeviceWind'}, {'intent': 'inform_room', 'entities': [{'address': '主卧'}]}, {'action': 'action_controlDeviceWind'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Wind', 'entities': [{'operation': '调高'}, {'device': '空调'}, {'target': '风速'}]}, {'action': 'action_controlDeviceWind'}, {'intent': 'inform_room', 'entities': [{'address': '主卧'}]}, {'action': 'action_controlDeviceWind'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Wind', 'entities': [{'operation': '调高'}, {'device': '空调'}, {'address': '主卧'}, {'target': '风速'}]}, {'action': 'action_controlDeviceWind'}]\n",
      "1\n",
      "/n\n",
      "[{'intent': 'Control-AC_Wind', 'entities': [{'operation': '调高'}, {'device': '空调'}, {'target': '风速'}]}, {'action': 'action_controlDeviceWind'}, {'intent': 'inform_ac', 'entities': [{'device': '空调'}, {'address': '主卧'}]}, {'action': 'action_controlDeviceWind'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'operation': '打开'}, {'device': '空调'}, {'address': '主卧'}]}, {'action': 'action_open_ac'}, {'intent': 'Control-AC_Wind', 'entities': [{'sensorvalue': '最大'}, {'target': '风速'}]}, {'action': 'action_controlDeviceWind'}, {'intent': 'Control-AC_Wind', 'entities': [{'sensorvalue': '最小'}, {'target': '风速'}]}, {'action': 'action_controlDeviceWind'}, {'intent': 'inform_ac', 'entities': [{'device': '空调'}, {'address': '主卧'}]}, {'action': 'action_controlDeviceWind'}]\n",
      "4\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'operation': '打开'}, {'device': '空调'}, {'address': '书房'}]}, {'action': 'action_open_ac'}, {'intent': 'Control-AC_Wind', 'entities': [{'device': '空调'}, {'address': '书房'}, {'operation': '增大'}, {'target': '风速'}]}, {'action': 'action_controlDeviceWind'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Temp', 'entities': [{'device': '空调'}, {'address': '主卧'}, {'operation': '调高到'}, {'temperature': '二十度'}]}, {'action': 'action_controlactemp'}]\n",
      "1\n",
      "/n\n",
      "[{'intent': 'Control-AC_Temp', 'entities': [{'address': '客厅'}, {'device': '空调'}, {'operation': '调高'}, {'temperature': '一度'}]}, {'action': 'action_controlactemp'}]\n",
      "1\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'device': '空调'}, {'address': '主卧'}, {'operation': '打开'}]}, {'action': 'action_open_ac'}, {'intent': 'Control-AC_Temp', 'entities': [{'device': '空调'}, {'operation': '调高到'}, {'temperature': '二十五度'}]}, {'action': 'action_controlactemp'}, {'intent': 'Control-AC_Temp', 'entities': [{'device': '空调'}, {'operation': '调高'}, {'temperature': '两度'}]}, {'action': 'action_controlactemp'}, {'intent': 'Control-AC_Close', 'entities': [{'operation': '关闭'}, {'device': '空调'}]}, {'action': 'action_close_ac'}]\n",
      "4\n",
      "/n\n",
      "[{'intent': 'Control-AC_Temp', 'entities': [{'device': '空调'}, {'operation': '调高到'}, {'temperature': '二十五度'}]}, {'action': 'action_controlactemp'}, {'intent': 'inform_ac', 'entities': [{'device': '空调'}, {'address': '主卧'}]}, {'action': 'action_controlactemp'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Temp', 'entities': [{'device': '空调'}, {'operation': '调高到'}, {'temperature': '二十五度'}]}, {'action': 'action_controlactemp'}, {'intent': 'inform_room', 'entities': [{'address': '主卧'}]}, {'action': 'action_controlactemp'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'operation': '打开'}, {'device': '空调'}]}, {'action': 'action_open_ac'}, {'intent': 'inform_ac', 'entities': [{'address': '主卧'}]}, {'action': 'action_open_ac'}, {'intent': 'Control-AC_Temp', 'entities': [{'temperature': '二十五度'}, {'target': '温度'}]}, {'action': 'action_controlactemp'}]\n",
      "3\n",
      "/n\n",
      "[{'intent': 'Control-AC_Open', 'entities': [{'operation': '打开'}, {'device': '空调'}]}, {'action': 'action_open_ac'}, {'intent': 'inform_ac', 'entities': [{'address': '客厅'}]}, {'action': 'action_open_ac'}, {'intent': 'Control-AC_Temp', 'entities': [{'target': '温度'}, {'operation': '调高'}]}, {'action': 'action_controlactemp'}]\n",
      "3\n",
      "/n\n",
      "[{'intent': 'Control-AC_Temp', 'entities': [{'device': '空调'}, {'target': '温度'}, {'operation': '调高'}]}, {'action': 'action_controlactemp'}, {'intent': 'inform_room', 'entities': [{'address': '主卧'}]}, {'action': 'action_controlactemp'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'latent_control_temp'}, {'action': 'action_latent_control_temp'}, {'intent': 'affirm'}, {'action': 'action_affirm_opensetaircondition'}]\n",
      "2\n",
      "/n\n",
      "[{'intent': 'latent_control_temp'}, {'action': 'action_latent_control_temp'}, {'intent': 'deny'}, {'action': 'action_deny_opensetaircondition'}]\n",
      "2\n",
      "/n\n"
     ]
    }
   ],
   "source": [
    "for i in dataset['stories']:\n",
    "    print(i['steps'])\n",
    "    print(len(i['steps'])//2)\n",
    "    print('/n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Construct dataset map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def construct_dataset(dataset):\n",
    "    # 将数据集的 intent entities 以及action 的类别抽取出来\n",
    "    intent_set = ['PAD']\n",
    "    entities_set = ['PAD']\n",
    "    action_set = ['PAD']\n",
    "    for v in dataset['stories']:\n",
    "        worth = v['steps']\n",
    "        for i in worth:\n",
    "            for key, val in i.items():\n",
    "                if key  == 'intent':\n",
    "                    intent_set.append(val)\n",
    "                if key == \"action\":\n",
    "                    action_set.append(val)\n",
    "                if key == 'entities':\n",
    "                    for i in val:\n",
    "                        for key , value in i.items():\n",
    "                            entities_set.append(key)\n",
    "    return set(intent_set), set(entities_set), set(action_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "intent: {'PAD', 'inform_range', 'Control-AC_Temp', 'Control-AC_Open', 'Control-AC_Timing', 'latent_control_temp', 'deny', 'inform_ac', 'Control-AC_Mode', 'affirm', 'inform_room', 'Control-AC_Close', 'Control-AC_Wind'} \n",
      " inetent_len : 13\n",
      "entities: {'PAD', 'temperature', 'address', 'range', 'mode', 'device', 'sensorvalue', 'time', 'target', 'operation'} \n",
      " entities_len : 10\n",
      "action: {'PAD', 'action_close_ac', 'action_latent_control_temp', 'action_controlactemp', 'actiming_form', 'action_controlDeviceWind', 'action_deny_opensetaircondition', 'action_open_ac', 'action_affirm_opensetaircondition', 'acmode_form'} \n",
      " action_len : 10\n"
     ]
    }
   ],
   "source": [
    "intent, entities, action = construct_dataset(dataset)\n",
    "print('intent: {} \\n inetent_len : {}'.format(intent,len(intent)))\n",
    "print('entities: {} \\n entities_len : {}'.format(entities,len(entities)))\n",
    "print('action: {} \\n action_len : {}'.format(action, len(action)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建意图索引\n",
    "intent2id = {}\n",
    "for index, val in enumerate(intent):\n",
    "    intent2id.update({val:index})\n",
    "\n",
    "id2intent = {}\n",
    "for index, val in enumerate(intent):\n",
    "    id2intent.update({index:val})  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建槽位索引\n",
    "entities2id = {}\n",
    "for index, val in enumerate(entities):\n",
    "    entities2id.update({val:index})\n",
    "\n",
    "id2entities = {}\n",
    "for index, val in enumerate(entities):\n",
    "    id2entities.update({index:val}) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建动作索引\n",
    "action2id = {}\n",
    "for index, val in enumerate(action):\n",
    "    action2id.update({val:index})\n",
    "\n",
    "id2action = {}\n",
    "for index, val in enumerate(action):\n",
    "    id2action.update({index:val})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将label2id 转为字典以json文件储存\n",
    "char = {}\n",
    "char.update({'action2id' : action2id})\n",
    "char.update({'id2action' : id2action})\n",
    "char.update({'intent2id' : intent2id})\n",
    "char.update({'id2intent' : id2intent})\n",
    "char.update({'entities2id' : entities2id})\n",
    "char.update({'id2entities' : id2entities})\n",
    "\n",
    "with open('./DM_char.json', mode='w', encoding='utf-8') as f:\n",
    "    json.dump(char, f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# extract dataset\n",
    "    - max_history 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def split_data(dataset):\n",
    "    # 以对话历史最长为3分割，以数组形式储存数据\n",
    "    data_set = []\n",
    "    num = 0\n",
    "    for val in dataset['stories']:\n",
    "        data = val['steps']\n",
    "        num += 1\n",
    "#         print(num)\n",
    "        for index in range(0,len(data),2):\n",
    "\n",
    "            previous_action = []\n",
    "            actions = []\n",
    "            slots = []\n",
    "            user_intent = []\n",
    "\n",
    "            # 当前状态\n",
    "            current_intent  =  data[index]['intent']\n",
    "            if 'entities' in data[index].keys():\n",
    "                current_slot = data[index]['entities']\n",
    "            elif 'entities' not in data[index].keys():\n",
    "                current_slot = []\n",
    "            current_action = data[index+1]['action']\n",
    "\n",
    "            # 前一时刻状态\n",
    "            pre1_intent = []\n",
    "            pre1_slot = []\n",
    "            pre1_action = []\n",
    "\n",
    "            # 前二时刻状态\n",
    "            pre2_intent = []\n",
    "            pre2_slot = []\n",
    "            pre2_action = [] \n",
    "\n",
    "            pre_history_1 = index-2\n",
    "            pre_history_2 = index-4\n",
    "\n",
    "            #判断对话历史是否存在\n",
    "            if pre_history_1 >= 0:\n",
    "                pre1_intent = data[pre_history_1]['intent']\n",
    "                if 'entities' in data[pre_history_1].keys():\n",
    "                    pre1_slot = data[pre_history_1]['entities']\n",
    "                pre1_action = data[pre_history_1+1]['action']\n",
    "\n",
    "            if pre_history_2 >= 0:\n",
    "                pre2_intent = data[pre_history_2]['intent']\n",
    "                if 'entities' in data[pre_history_2].keys():\n",
    "                    pre2_slot = data[pre_history_2]['entities']\n",
    "                pre2_action = data[pre_history_2+1]['action'] \n",
    "        \n",
    "            previous_action_sum = [pre1_action, pre2_action]\n",
    "            for i in previous_action_sum:\n",
    "                if i != []:\n",
    "                    previous_action.append(i)\n",
    "            if previous_action == []:\n",
    "                previous_action = ['PAD']\n",
    "#             print('previous_action: ', previous_action)\n",
    "\n",
    "            \n",
    "            actions = [current_action]\n",
    "#             print('actions: ',actions)\n",
    "            \n",
    "            slots_sum = [current_slot , pre1_slot , pre2_slot]            \n",
    "            for i in slots_sum:\n",
    "                if i  != []:\n",
    "                    for val in i:\n",
    "                        for key, j in val.items():\n",
    "                            if key not in slots:\n",
    "                                slots.append((key))\n",
    "            if slots == []:\n",
    "                slots = ['PAD']\n",
    "#             print('slots:',slots)\n",
    "            \n",
    "            \n",
    "            user_intent_sum= [current_intent , pre1_intent , pre2_intent]\n",
    "            for i in user_intent_sum:\n",
    "                if i != []:\n",
    "                    user_intent.append(i)\n",
    "            if user_intent == []:\n",
    "                user_intent = ['PAD']\n",
    "#             print('user_intent: ',user_intent)\n",
    "\n",
    "            data_set.append({'previous_action':previous_action, 'slots':slots,'user_intent':user_intent, 'action':actions})\n",
    "    return data_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "data_set = split_data(predataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'previous_action': ['PAD'],\n",
       "  'slots': ['address', 'device', 'operation'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['device', 'operation'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac'],\n",
       "  'slots': ['address', 'device', 'operation'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['device', 'operation'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac'],\n",
       "  'slots': ['address', 'device', 'operation'],\n",
       "  'user_intent': ['inform_room', 'Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['device', 'operation'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac'],\n",
       "  'slots': ['range', 'device', 'operation'],\n",
       "  'user_intent': ['inform_range', 'Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['range', 'device', 'operation'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac'],\n",
       "  'slots': ['device', 'address', 'operation'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac'],\n",
       "  'slots': ['address', 'operation', 'device'],\n",
       "  'user_intent': ['inform_room', 'Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['address', 'device', 'operation'],\n",
       "  'user_intent': ['Control-AC_Close'],\n",
       "  'action': ['action_close_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['device', 'operation'],\n",
       "  'user_intent': ['Control-AC_Close'],\n",
       "  'action': ['action_close_ac']},\n",
       " {'previous_action': ['action_close_ac'],\n",
       "  'slots': ['address', 'device', 'operation'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Close'],\n",
       "  'action': ['action_close_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['range', 'device', 'operation'],\n",
       "  'user_intent': ['Control-AC_Close'],\n",
       "  'action': ['action_close_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Close'],\n",
       "  'action': ['action_close_ac']},\n",
       " {'previous_action': ['action_close_ac'],\n",
       "  'slots': ['device', 'address', 'operation'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Close'],\n",
       "  'action': ['action_close_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Close'],\n",
       "  'action': ['action_close_ac']},\n",
       " {'previous_action': ['action_close_ac'],\n",
       "  'slots': ['address', 'operation', 'device'],\n",
       "  'user_intent': ['inform_room', 'Control-AC_Close'],\n",
       "  'action': ['action_close_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Close'],\n",
       "  'action': ['action_close_ac']},\n",
       " {'previous_action': ['action_close_ac'],\n",
       "  'slots': ['range', 'operation', 'device'],\n",
       "  'user_intent': ['inform_range', 'Control-AC_Close'],\n",
       "  'action': ['action_close_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['address', 'device', 'mode'],\n",
       "  'user_intent': ['Control-AC_Mode'],\n",
       "  'action': ['acmode_form']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['mode'],\n",
       "  'user_intent': ['Control-AC_Mode'],\n",
       "  'action': ['acmode_form']},\n",
       " {'previous_action': ['acmode_form'],\n",
       "  'slots': ['address', 'device', 'mode'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Mode'],\n",
       "  'action': ['acmode_form']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['device', 'mode'],\n",
       "  'user_intent': ['Control-AC_Mode'],\n",
       "  'action': ['acmode_form']},\n",
       " {'previous_action': ['acmode_form'],\n",
       "  'slots': ['address', 'device', 'mode'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Mode'],\n",
       "  'action': ['acmode_form']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device', 'address'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac'],\n",
       "  'slots': ['mode', 'operation', 'device', 'address'],\n",
       "  'user_intent': ['Control-AC_Mode', 'Control-AC_Open'],\n",
       "  'action': ['acmode_form']},\n",
       " {'previous_action': ['acmode_form', 'action_open_ac'],\n",
       "  'slots': ['mode', 'operation', 'device', 'address'],\n",
       "  'user_intent': ['Control-AC_Mode', 'Control-AC_Mode', 'Control-AC_Open'],\n",
       "  'action': ['acmode_form']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['time', 'operation', 'device', 'address'],\n",
       "  'user_intent': ['Control-AC_Timing'],\n",
       "  'action': ['actiming_form']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['time', 'operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Timing'],\n",
       "  'action': ['actiming_form']},\n",
       " {'previous_action': ['actiming_form'],\n",
       "  'slots': ['device', 'address', 'time', 'operation'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Timing'],\n",
       "  'action': ['actiming_form']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['time', 'operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Timing'],\n",
       "  'action': ['actiming_form']},\n",
       " {'previous_action': ['actiming_form'],\n",
       "  'slots': ['address', 'time', 'operation', 'device'],\n",
       "  'user_intent': ['inform_room', 'Control-AC_Timing'],\n",
       "  'action': ['actiming_form']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['PAD'],\n",
       "  'user_intent': ['Control-AC_Timing'],\n",
       "  'action': ['actiming_form']},\n",
       " {'previous_action': ['actiming_form'],\n",
       "  'slots': ['time', 'operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Timing', 'Control-AC_Timing'],\n",
       "  'action': ['actiming_form']},\n",
       " {'previous_action': ['actiming_form', 'actiming_form'],\n",
       "  'slots': ['device', 'address', 'time', 'operation'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Timing', 'Control-AC_Timing'],\n",
       "  'action': ['actiming_form']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['time', 'operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Timing'],\n",
       "  'action': ['actiming_form']},\n",
       " {'previous_action': ['actiming_form'],\n",
       "  'slots': ['address', 'time', 'operation', 'device'],\n",
       "  'user_intent': ['inform_room', 'Control-AC_Timing'],\n",
       "  'action': ['actiming_form']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac'],\n",
       "  'slots': ['address', 'operation', 'device'],\n",
       "  'user_intent': ['inform_room', 'Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac', 'action_open_ac'],\n",
       "  'slots': ['time', 'operation', 'address', 'device'],\n",
       "  'user_intent': ['Control-AC_Timing', 'inform_room', 'Control-AC_Open'],\n",
       "  'action': ['actiming_form']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'target', 'device', 'address'],\n",
       "  'user_intent': ['Control-AC_Wind'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['sensorvalue', 'target', 'device', 'address'],\n",
       "  'user_intent': ['Control-AC_Wind'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'target', 'device'],\n",
       "  'user_intent': ['Control-AC_Wind'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['action_controlDeviceWind'],\n",
       "  'slots': ['address', 'operation', 'target', 'device'],\n",
       "  'user_intent': ['inform_room', 'Control-AC_Wind'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device', 'target'],\n",
       "  'user_intent': ['Control-AC_Wind'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['action_controlDeviceWind'],\n",
       "  'slots': ['address', 'operation', 'device', 'target'],\n",
       "  'user_intent': ['inform_room', 'Control-AC_Wind'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device', 'address', 'target'],\n",
       "  'user_intent': ['Control-AC_Wind'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device', 'target'],\n",
       "  'user_intent': ['Control-AC_Wind'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['action_controlDeviceWind'],\n",
       "  'slots': ['device', 'address', 'operation', 'target'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Wind'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device', 'address'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac'],\n",
       "  'slots': ['sensorvalue', 'target', 'operation', 'device', 'address'],\n",
       "  'user_intent': ['Control-AC_Wind', 'Control-AC_Open'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['action_controlDeviceWind', 'action_open_ac'],\n",
       "  'slots': ['sensorvalue', 'target', 'operation', 'device', 'address'],\n",
       "  'user_intent': ['Control-AC_Wind', 'Control-AC_Wind', 'Control-AC_Open'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['action_controlDeviceWind', 'action_controlDeviceWind'],\n",
       "  'slots': ['device', 'address', 'sensorvalue', 'target'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Wind', 'Control-AC_Wind'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device', 'address'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac'],\n",
       "  'slots': ['device', 'address', 'operation', 'target'],\n",
       "  'user_intent': ['Control-AC_Wind', 'Control-AC_Open'],\n",
       "  'action': ['action_controlDeviceWind']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['device', 'address', 'operation', 'temperature'],\n",
       "  'user_intent': ['Control-AC_Temp'],\n",
       "  'action': ['action_controlactemp']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['address', 'device', 'operation', 'temperature'],\n",
       "  'user_intent': ['Control-AC_Temp'],\n",
       "  'action': ['action_controlactemp']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['device', 'address', 'operation'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac'],\n",
       "  'slots': ['device', 'operation', 'temperature', 'address'],\n",
       "  'user_intent': ['Control-AC_Temp', 'Control-AC_Open'],\n",
       "  'action': ['action_controlactemp']},\n",
       " {'previous_action': ['action_controlactemp', 'action_open_ac'],\n",
       "  'slots': ['device', 'operation', 'temperature', 'address'],\n",
       "  'user_intent': ['Control-AC_Temp', 'Control-AC_Temp', 'Control-AC_Open'],\n",
       "  'action': ['action_controlactemp']},\n",
       " {'previous_action': ['action_controlactemp', 'action_controlactemp'],\n",
       "  'slots': ['operation', 'device', 'temperature'],\n",
       "  'user_intent': ['Control-AC_Close', 'Control-AC_Temp', 'Control-AC_Temp'],\n",
       "  'action': ['action_close_ac']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['device', 'operation', 'temperature'],\n",
       "  'user_intent': ['Control-AC_Temp'],\n",
       "  'action': ['action_controlactemp']},\n",
       " {'previous_action': ['action_controlactemp'],\n",
       "  'slots': ['device', 'address', 'operation', 'temperature'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Temp'],\n",
       "  'action': ['action_controlactemp']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['device', 'operation', 'temperature'],\n",
       "  'user_intent': ['Control-AC_Temp'],\n",
       "  'action': ['action_controlactemp']},\n",
       " {'previous_action': ['action_controlactemp'],\n",
       "  'slots': ['address', 'device', 'operation', 'temperature'],\n",
       "  'user_intent': ['inform_room', 'Control-AC_Temp'],\n",
       "  'action': ['action_controlactemp']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac'],\n",
       "  'slots': ['address', 'operation', 'device'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac', 'action_open_ac'],\n",
       "  'slots': ['temperature', 'target', 'address', 'operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Temp', 'inform_ac', 'Control-AC_Open'],\n",
       "  'action': ['action_controlactemp']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['operation', 'device'],\n",
       "  'user_intent': ['Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac'],\n",
       "  'slots': ['address', 'operation', 'device'],\n",
       "  'user_intent': ['inform_ac', 'Control-AC_Open'],\n",
       "  'action': ['action_open_ac']},\n",
       " {'previous_action': ['action_open_ac', 'action_open_ac'],\n",
       "  'slots': ['target', 'operation', 'address', 'device'],\n",
       "  'user_intent': ['Control-AC_Temp', 'inform_ac', 'Control-AC_Open'],\n",
       "  'action': ['action_controlactemp']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['device', 'target', 'operation'],\n",
       "  'user_intent': ['Control-AC_Temp'],\n",
       "  'action': ['action_controlactemp']},\n",
       " {'previous_action': ['action_controlactemp'],\n",
       "  'slots': ['address', 'device', 'target', 'operation'],\n",
       "  'user_intent': ['inform_room', 'Control-AC_Temp'],\n",
       "  'action': ['action_controlactemp']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['PAD'],\n",
       "  'user_intent': ['latent_control_temp'],\n",
       "  'action': ['action_latent_control_temp']},\n",
       " {'previous_action': ['action_latent_control_temp'],\n",
       "  'slots': ['PAD'],\n",
       "  'user_intent': ['affirm', 'latent_control_temp'],\n",
       "  'action': ['action_affirm_opensetaircondition']},\n",
       " {'previous_action': ['PAD'],\n",
       "  'slots': ['PAD'],\n",
       "  'user_intent': ['latent_control_temp'],\n",
       "  'action': ['action_latent_control_temp']},\n",
       " {'previous_action': ['action_latent_control_temp'],\n",
       "  'slots': ['PAD'],\n",
       "  'user_intent': ['deny', 'latent_control_temp'],\n",
       "  'action': ['action_deny_opensetaircondition']}]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def trans2labelid(vocab,x):\n",
    "        max_len = len(vocab)\n",
    "        labels = [vocab[label] for label in x]\n",
    "        label_onehot = np.eye(max_len)[labels]\n",
    "        values = sum(label_onehot)\n",
    "        return values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_conv_data(data_set):\n",
    "    dataset_previous_action = []\n",
    "    dataset_slots = []\n",
    "    dataset_user_intent = [ValueError: 'previous_action_inputs ' is not a valid scope name]\n",
    "    dataset_action = []\n",
    "    for i in data_set:\n",
    "        previous_action = i['previous_action']\n",
    "        dataset_previous_action.append(trans2labelid(action2id,previous_action))\n",
    "        slots = i['slots']\n",
    "        dataset_slots.append(trans2labelid(entities2id,slots))\n",
    "        user_intent = i['user_intent']\n",
    "        dataset_user_intent.append(trans2labelid(intent2id,user_intent))\n",
    "        action = i['action']\n",
    "        dataset_action.append(trans2labelid(action2id,action))\n",
    "    \n",
    "    return np.array(dataset_previous_action), np.array(dataset_slots), \\\n",
    "            np.array(dataset_user_intent), np.array(dataset_action)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "previous_action, slots, user_intent, action = extract_conv_data(data_set) "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
